<template>
  <view class="warp">
    <!-- 商品列表 -->
    <view class="goods-list">
      <view class="tis" v-if="goodsList.length == 0"><u-empty mode="car" /></view>
      <view class="row" v-for="(row, index) in goodsList" :key="index">
        <!-- 商品 -->
        <view class="carrier">
          <!-- checkbox -->
          <view class="checkbox-box" @tap="selectStatus(row)">
            <view class="checkbox"><view :class="[row.selected ? 'on' : '']"></view></view>
          </view>
          <!-- 商品信息 -->
          <view class="goods-info" @tap="$u.route(`/pages/goods/goods?id=${row.goods_id}`)">
            <image class="img" :src="row.goods_thumbnail"></image>
            <view class="info">
              <view class="title">{{ row.goods_name }}</view>
              <view class="price-number">
                <view class="price">￥{{ row.goods_price }}</view>
                <view class="number">
                  <view class="sub" @tap.stop="changeQuantity(row, -1)"><u-icon name="minus" size="30" color="#000"></u-icon></view>
                  <view class="input" @click.stop>{{ row.goods_quantity }}</view>
                  <view class="add" @tap.stop="changeQuantity(row, 1)"><u-icon name="plus" size="30" color="#000"></u-icon></view>
                </view>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>
    <!-- 底部菜单 -->
    <view class="footer" :style="{ bottom: footerbottom }">
      <view class="checkbox-box" @tap="allSelect">
        <view class="checkbox"><view :class="[isAllselected ? 'on' : '']"></view></view>
        <view class="text">全选</view>
      </view>
      <view class="delBtn" @tap="deleteList" v-if="selectedList.length > 0">删除</view>
      <view class="settlement">
        <view class="sum">
          合计:
          <view class="money">￥{{ amount }}</view>
        </view>
        <view class="btn" @tap="toConfirmation">结算({{ selectedList.length }})</view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      amount: '0.00',
      goodsList: [],
      footerbottom: 0
    };
  },
  computed: {
    // 选中的商品
    selectedList() {
      let arr = [];
      this.goodsList.forEach(row => {
        if (row.selected) {
          arr.push(row);
        }
      });
      return arr;
    },
    isAllselected() {
      return this.selectedList.length === this.goodsList.length;
    }
  },
  onLoad() {
    //兼容H5结算条位置
    // #ifdef H5
    this.footerbottom = document.getElementsByTagName('uni-tabbar')[0].offsetHeight + 'px';
    // #endif
  },
  onShow() {
    if (this.$isLogin()) {
      this.getData();
    }
  },
  methods: {
    getData() {
      this.$api.get({
        url: 'order/cart/index',
        success: res => {
          if (res.code === 1) {
            this.amount = res.data.amount;
            this.goodsList = res.data.cart || [];
          } else {
            this.$toast(res.msg);
          }
        }
      });
    },
    //跳转确认订单页面
    toConfirmation() {
      if (this.selectedList.length > 0) {
        uni.navigateTo({
          url: '../pay/preOrder/preOrder'
        });
      }
    },
    // 删除商品
    deleteList() {
      let ids = [];
      this.selectedList.forEach(row => {
        ids.push(row.id);
      });
      this.$api.post({
        url: 'order/cart/cancel',
        data: {
          ids: ids.join(',')
        },
        success: res => {
          this.$toast(res.msg);
          if (res.code === 1) {
            this.getData();
          }
        },
        complete: () => {
          uni.hideLoading();
        }
      });
    },
    // 选中商品
    selectStatus(row) {
      this.$loading();
      let selected = row.selected > 0 ? 0 : 1;
      this.$api.post({
        url: 'order/cart/selectStatus',
        data: {
          id: row.id,
          selected: selected
        },
        success: res => {
          if (res.code === 1) {
            this.amount = res.data.amount;
            row.selected = selected;
          } else {
            this.$toast(res.msg);
          }
        },
        complete: () => {
          uni.hideLoading();
        }
      });
    },
    //全选
    allSelect() {
      if (this.$isLogin()) {
        this.$loading();
        let selected = this.isAllselected ? 0 : 1;
        this.$api.post({
          url: 'order/cart/selectAllStatus',
          data: {
            selected: selected
          },
          success: res => {
            if (res.code === 1) {
              this.amount = res.data.amount;
              this.goodsList.forEach(row => {
                row.selected = selected;
              });
            } else {
              this.$toast(res.msg);
            }
          },
          complete: () => {
            uni.hideLoading();
          }
        });
      }
    },
    changeQuantity(row, quantity) {
      let num = row.goods_quantity + quantity;
      if (num <= 0) {
        return;
      }
      this.$loading();
      this.$api.post({
        url: 'order/cart/changeQuantity',
        data: {
          id: row.id,
          quantity: num
        },
        success: res => {
          if (res.code === 1) {
            this.amount = res.data.amount;
            row.goods_quantity = num;
          } else {
            this.$toast(res.msg);
          }
        },
        complete: () => {
          uni.hideLoading();
        }
      });
    }
  }
};
</script>
<style lang="scss">
.checkbox-box {
  display: flex;
  align-items: center;
  .checkbox {
    width: 35rpx;
    height: 35rpx;
    border-radius: 100%;
    border: solid 2rpx #f06c7a;
    display: flex;
    justify-content: center;
    align-items: center;
    .on {
      width: 25rpx;
      height: 25rpx;
      border-radius: 100%;
      background-color: #f06c7a;
    }
  }
  .text {
    font-size: 28rpx;
    margin-left: 10rpx;
  }
}
.goods-list {
  width: 750rpx;
  padding: 20rpx 0 120rpx 0;
  .tis {
    height: 60vh;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 32rpx;
  }
  .row {
    width: 690rpx;
    height: 220rpx;
    margin: 20rpx auto;
    border-radius: 15rpx;
    box-shadow: 0rpx 5rpx 20rpx rgba(0, 0, 0, 0.1);
    display: flex;
    align-items: center;
    position: relative;
    overflow: hidden;
    z-index: 4;
    border: 0;
    .carrier {
      .checkbox-box {
        padding-left: 20rpx;
        flex-shrink: 0;
        height: 22vw;
        margin-right: 20rpx;
      }
      position: absolute;
      width: 100%;
      height: 100%;
      z-index: 3;
      display: flex;
      align-items: center;
      .goods-info {
        width: 100%;
        display: flex;
        padding-right: 20rpx;
        .img {
          width: 22vw;
          height: 22vw;
          border-radius: 10rpx;
          overflow: hidden;
          flex-shrink: 0;
          margin-right: 10rpx;
          image {
            width: 22vw;
            height: 22vw;
          }
        }
        .info {
          width: 100%;
          overflow: hidden;
          display: flex;
          flex-wrap: wrap;
          position: relative;
          margin-left: 20rpx;
          .title {
            width: 100%;
            font-size: 28rpx;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 2;
            overflow: hidden;
          }
          .price-number {
            position: absolute;
            width: 100%;
            bottom: 0rpx;
            display: flex;
            justify-content: space-between;
            align-items: flex-end;
            font-size: 28rpx;
            height: 60rpx;
            .price {
              color: #f40;
              font-weight: bold;
            }
            .number {
              display: flex;
              justify-content: center;
              align-items: center;
              .input {
                width: 60rpx;
                height: 60rpx;
                display: flex;
                justify-content: center;
                align-items: center;
                text-align: center;
                font-size: 26rpx;
              }
              .sub,
              .add {
                width: 45rpx;
                height: 45rpx;
                background-color: #f3f3f3;
                border-radius: 5rpx;
                display: flex;
                align-items: center;
                justify-content: center;
              }
            }
          }
        }
      }
    }
  }
}
.footer {
  width: 750rpx;
  padding: 0 20rpx;
  background-color: #fbfbfb;
  height: 100rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 28rpx;
  position: fixed;
  bottom: 0rpx;
  z-index: 5;
  .delBtn {
    border: solid 1rpx #f06c7a;
    color: #f06c7a;
    padding: 0 30rpx;
    height: 50rpx;
    border-radius: 30rpx;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .settlement {
    width: 60%;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    .sum {
      width: 50%;
      font-size: 28rpx;
      margin-right: 10rpx;
      display: flex;
      justify-content: flex-end;
      .money {
        color: #f40;
        font-weight: 600;
      }
    }
    .btn {
      padding: 0 30rpx;
      height: 50rpx;
      background-color: #f06c7a;
      color: #fff;
      display: flex;
      justify-content: center;
      align-items: center;
      border-radius: 30rpx;
    }
  }
}
</style>
